package cst.a722wr.zyfypt_722wr.Adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a722wr.zyfypt_722wr.Activity.TcaseActivity;
import cst.a722wr.zyfypt_722wr.Bean.CollectResult;
import cst.a722wr.zyfypt_722wr.Bean.TcaseBean;
import cst.a722wr.zyfypt_722wr.R;

public class CTcaseAdapter extends RecyclerView.Adapter {
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<CollectResult<TcaseBean>>  list;//保存要显示的数据

    public CTcaseAdapter(Context context) {
        this.context = context;
        layoutInflater=LayoutInflater.from(context);
    }

    public void setList(List<CollectResult<TcaseBean>> list)
    {
        this.list=list;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView insert_time;
        TextView description;
        ImageView thumb;

        public ViewHolder(View itemView) {
            super(itemView);
            thumb = (ImageView) itemView
                    .findViewById(R.id.item_thumb);
            name = (TextView) itemView
                    .findViewById(R.id.item_name);
            insert_time = (TextView) itemView
                    .findViewById(R.id.item_insert_time);
            description = (TextView) itemView
                    .findViewById(R.id.item_description);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TcaseBean  bean = list.get(position).getBean();
        if(bean==null) return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.thumb.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.tware_default));
        viewHolder.name.setText(bean.getName());
        viewHolder.insert_time.setText(bean.getUpdate_time());
        viewHolder.description.setText(getLimitLengthString(bean.getDescription(),58,"..."));

        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+bean.getThumb())
                .placeholder(R.drawable.tware_default)
                .into(viewHolder.thumb);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, TcaseActivity.class);
                intent.putExtra("resid", list.get(position).getBean().getId());
                intent.putExtra("userid", list.get(position).getBean().getUserid());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }

    private String getLimitLengthString(String str,int len, String symbol){
        if(str.length()<len)
            return str;
        else{
            return str.substring(0,len-1)+symbol;
        }
    }

}

